<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>jhq06</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  </head>
  <body>
    <div>
      <div id="timeNow">当前时间：0000-00-00 00:00:00</div>
      <form name="registerMessage">
        <label for="account">账号：</label>
        <input type="text" name="account" />
        <br />
        <label for="password">密码：</label>
        <input type="password" name="password" />
        <br />
        <input type="submit" value="注册" name="submit" />
        <input type="button" id="fetchUsersList" value="获取用户列表" />
      </form>
      <ul id="usernameList"></ul>
    </div>
    <script>
      (() => {
        //console.log("jjj");
        //alert(document.getElementById("timeNow").innerText)
        //alert(1);

        axios({
          method: "get",
          url: "https://f.m.suning.com/api/ct.do",
          timeout: 20000,
        })
          .then((res) => {
            if (
              res.data.api === "time" &&
              res.data.code === "1" &&
              res.data.msg === ""
            ) {
              console.log(res.data);
              let currentTime = res.data.currentTime;
              let date = new Date(currentTime);
              document.getElementById("timeNow").innerText =
                "当前时间：" + date.toLocaleString();
            } else alert("now响应错误");
          })
          .catch((err) => {
            alert(err.code);
            if (err.code === "ECONNABORTED") {
              console.error("now的请求超时了悲");
            } else if (err.response) {
              console.error("now的响应有错误悲:", err.response.status);
            } else if (err.request) {
              console.error("发了now的请求没响应，不知道咋办悲:", err.request);
            } else {
              console.error("now的请求就不太对悲:", err.message);
            }
          });
      })();
      (() => {
        let registerForm = document.forms["registerMessage"];
        let data = {
          username: "",
          password: "",
        };
        registerForm.elements["submit"].onclick = () => {
          data.username = registerForm.elements["account"].value;
          data.password = registerForm.elements["password"].value;
          //alert(data.password);
          axios({
            method: "post",
            url: "http://43.143.169.168:9090/user/register",
            data: data,
          })
            .then((res) => {
              if (
                res.data.api === "time" &&
                res.data.code === "1" &&
                res.data.msg === ""
              )
                console.log("注册报文" + res);
              else alert("响应错误");
            })
            .catch((err) => {
              alert(err.code);
              if (err.code === "ECONNABORTED") {
                console.error("register的请求超时了悲");
              } else if (err.response) {
                console.error("regist的响应有错误悲:", err.response.status);
              } else if (err.request) {
                console.error(
                  "发了registe的请求没响应，不知道咋办悲:",
                  err.request
                );
              } else {
                console.error("regist的请求就不太对悲:", err.message);
              }
            });
        };
      })();
      (() => {
        document.getElementById("fetchUsersList").onclick = () => {
          axios({
            method: "get",
            url: "http://43.143.169.168:9090/user/all",
          })
            .then((res) => {
              if (res.data.code === "200" && res.data.msg === "成功")
                res.data.forEach((name) => {
                  const listItem = document.createElement("li");
                  listItem.textContent = name;
                  list.appendChild(listItem);
                  console.log("用户信息列表报文" + res);
                });
              else alert("all响应错误");
            })
            .catch((err) => {
              if (err.code === "ECONNABORTED") {
                console.error("all的请求超时了悲");
              } else if (err.response) {
                console.error("all的响应有错误悲:", err.response.status);
              } else if (err.request) {
                console.error(
                  "发了all的请求没响应，不知道咋办悲:",
                  err.request
                );
              } else {
                console.error("all的请求就不太对悲:", err.message);
              }
            });
        };
      })();
    </script>
  </body>
</html>
